package cn.disicn.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import cn.disicn.common.Const;
import cn.disicn.common.ServerResponse;
import cn.disicn.model.AppVersion;
import cn.disicn.model.SecAdminUser;
import cn.disicn.util.IDUtil;

import com.jfinal.core.Controller;

/**
 * @author zjk
 * @date 2018年1月3日 下午4:59:52
 * @desc
 * 
 */
public class AppVersionController extends Controller {

	private static final AppVersion appVersion = new AppVersion();
	private static final SecAdminUser secAdminuser = new SecAdminUser();

	/**
	 * 
	 * @desc 查询版本记录
	 */
	public void index() {
		Integer pageNum = getParaToInt("pageNum");
		Integer pageSize = getParaToInt("pageSize");
		String status = getPara("status");
		if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
			renderJson(appVersion.getAppVersionList(pageNum, pageSize, status));
			return;
		}
		renderJson(ServerResponse
				.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 添加版本
	 */
	public void addAppVersion() {
		String id = getPara("id");
		String title = getPara("title");
		String version = getPara("version");
		String force = getPara("force");
		String status = getPara("status");
		String remark = getPara("remark");
		String url = getPara("url");
		String code = getPara("code");
		if (!StringUtils.isBlank(code) && code.length() == 6) {
			ServerResponse<String> response = secAdminuser.valiImgCode(code,
					getSession());
			if (response.isSuccess()) {
				if (!StringUtils.isBlank(id) && id.length() == 32) {
					if (!StringUtils.isBlank(Const.AppVersionForceEnum.codeOf(
							Integer.parseInt(force)).getValue())
							&& !StringUtils.isBlank(Const.AppVersionStatusEnum
									.codeOf(Integer.parseInt(status))
									.getValue())) {
						appVersion.setId(id);
						appVersion.setTitle(title);
						appVersion.setVersion(version);
						appVersion.setTitle(title);
						appVersion.setForce(force);
						appVersion.setStatus(status);
						appVersion.setUrl(url);
						appVersion.setRemark(remark);
						appVersion.setUpdateTime(new Date());
						renderJson(appVersion.updAppVersion(appVersion));
					} else {
						renderJson(ServerResponse
								.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
					}
				} else {
					if (!StringUtils.isBlank(Const.AppVersionForceEnum.codeOf(
							Integer.parseInt(force)).getValue())
							&& !StringUtils.isBlank(Const.AppVersionStatusEnum
									.codeOf(Integer.parseInt(status))
									.getValue())) {
						appVersion.setId(IDUtil.getId());
						appVersion.setTitle(title);
						appVersion.setVersion(version);
						appVersion.setTitle(title);
						appVersion.setForce(force);
						appVersion.setStatus(status);
						appVersion.setUrl(url);
						appVersion.setRemark(remark);
						appVersion.setCreateTime(new Date());
						renderJson(appVersion.addAppVersion(appVersion));
					} else {
						renderJson(ServerResponse
								.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
					}
				}
			} else {
				renderJson(response);
			}
			return;
		}
		renderJson(ServerResponse
				.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 删除版本
	 */
	public void delAppVersion() {
		String id = getPara("id");
		if (!StringUtils.isBlank(id) && id.length() == 32) {
			renderJson(appVersion.delAppVersion(id));
			return;
		}
		renderJson(ServerResponse
				.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}
}
